package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.WylQzqy;
import com.corpcredit.service.GenericManager;

public class WylQzqyForm extends BasePage implements Serializable {
    private GenericManager<WylQzqy, Long> wylQzqyManager;
    private WylQzqy wylQzqy = new WylQzqy();
    private Long id;

    public void setWylQzqyManager(GenericManager<WylQzqy, Long> wylQzqyManager) {
        this.wylQzqyManager = wylQzqyManager;
    }

    public WylQzqy getWylQzqy() {
        return wylQzqy;
    }

    public void setWylQzqy(WylQzqy wylQzqy) {
        this.wylQzqy = wylQzqy;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        wylQzqyManager.remove(wylQzqy.getId());
        addMessage("wylQzqy.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            wylQzqy = wylQzqyManager.get(id);
        } else {
            wylQzqy = new WylQzqy();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (wylQzqy.getId() == null || wylQzqy.getId() == 0);
        // set username who act this operation, get the user name from httprequest
        wylQzqy.setUsername( getRequest().getRemoteUser() );
        // set operation date using the current time
        wylQzqy.setOptdate( new Date() );
        // set the operation type, default two types: add and update
        wylQzqy.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        wylQzqyManager.save(wylQzqy);

        String key = (isNew) ? "wylQzqy.added" : "wylQzqy.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            // set the id to the next page
            this.id = wylQzqy.getId();
            // return the update confirmation page
            return edit();
        }
    }
} 